<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="病例uuid" prop="caseUuid">
        <el-input v-model="queryParams.caseUuid" placeholder="请输入病例uuid" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="病例编号6" prop="caseCode6">
        <el-input v-model="queryParams.caseCode6" placeholder="请输入病例编号6" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="所属问诊人open" prop="caseOpenid">
        <el-input v-model="queryParams.caseOpenid" placeholder="请输入所属问诊人open" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="问诊人手机号" prop="casePhone">
        <el-input v-model="queryParams.casePhone" placeholder="请输入所属问诊人phone" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="问诊人姓名" prop="caseUname">
        <el-input v-model="queryParams.caseUname" placeholder="请输入问诊人姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="问诊人年龄" prop="caseUage">
        <el-input v-model="queryParams.caseUage" placeholder="请输入问诊人年龄" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="病例备注" prop="caseRemark">
        <el-input v-model="queryParams.caseRemark" placeholder="请输入病例备注" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <!-- <el-form-item label="正面人体模型" prop="caseFrontpoint">
        <el-input v-model="queryParams.caseFrontpoint" placeholder="请输入问诊人体模型" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="反面人体模型" prop="caseBackpoint">
        <el-input v-model="queryParams.caseBackpoint" placeholder="请输入问诊人体模型" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['ask:case:add']">新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['ask:case:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['ask:case:remove']">删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['ask:case:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="caseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="病例编号" align="center" prop="caseId" />
      <!-- <el-table-column label="病例uuid" align="center" prop="caseUuid" />  -->
      <el-table-column label="病例编号" align="center" prop="caseCode6" />
      <!-- <el-table-column label="所属问诊人open" align="center" prop="caseOpenid" /> -->
      <el-table-column label="所属问诊人手机号" align="center" prop="casePhone" />
      <el-table-column label="问诊人姓名" align="center" prop="caseUname" />
      <el-table-column label="问诊人年龄" align="center" prop="caseUage" />
      <!-- <el-table-column label="问诊人性别" align="center" prop="caseUsex" /> -->
      <el-table-column label="问诊人性别" align="center" prop="caseUsex_Text" />
      <el-table-column label="病例详情" align="center" prop="caseText" />
      <el-table-column label="作为案例" align="center" prop="caseRemark" />
      <el-table-column label="关联渠道" align="center" prop="channelName" />
      <!-- <el-table-column label="问诊人体模型" align="center" prop="caseFrontpoint" />
      <el-table-column label="问诊人体模型" align="center" prop="caseBackpoint" /> -->
      <el-table-column label="治理前照片" align="center" prop="caseImagebefore" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.caseImagebefore" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="治理后照片" align="center" prop="caseImageafter" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.caseImageafter" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['ask:case:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['ask:case:remove']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="goTreat(scope.row)">医疗</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改病例管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="病例uuid" prop="caseUuid">
          <el-input v-model="form.caseUuid" placeholder="请输入病例uuid" />
        </el-form-item>
        <el-form-item label="病例编号6" prop="caseCode6">
          <el-input v-model="form.caseCode6" placeholder="请输入病例编号6" />
        </el-form-item>
        <el-form-item label="所属问诊人open" prop="caseOpenid">
          <el-input v-model="form.caseOpenid" placeholder="请输入所属问诊人open" />
        </el-form-item> -->
        <el-form-item label="所属问诊人手机号" prop="casePhone">
          <el-input v-model="form.casePhone" placeholder="请输入所属问诊人phone" />
        </el-form-item>
        <el-form-item label="问诊人姓名" prop="caseUname">
          <el-input v-model="form.caseUname" placeholder="请输入问诊人姓名" />
        </el-form-item>
        <el-form-item label="问诊人年龄" prop="caseUage">
          <el-input v-model="form.caseUage" placeholder="请输入问诊人年龄" />
        </el-form-item>

        <!-- <el-form-item label="问诊人性别" prop="caseUsex">
          <el-input v-model="form.caseUsex" placeholder="请输入问诊人年龄" />
        </el-form-item> -->

        <el-form-item label="病例详情" prop="caseText">
          <el-input v-model="form.caseText" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="病例备注" prop="caseRemark">
          <el-input v-model="form.caseRemark" placeholder="请输入病例备注" />
        </el-form-item>
        <el-form-item label="关联渠道" prop="caseChannelid">
        <el-select v-model="form.caseChannelid" placeholder="请选择关联渠道" clearable>
          <el-option v-for="item in channelOptions" :key="item.channelId" :label="item.channelName" :value="item.channelId" />
        </el-select>
      </el-form-item>
        <!-- <el-form-item label="问诊人体模型" prop="caseFrontpoint">
          <el-input v-model="form.caseFrontpoint" placeholder="请输入问诊人体模型" />
        </el-form-item>
        <el-form-item label="问诊人体模型" prop="caseBackpoint">
          <el-input v-model="form.caseBackpoint" placeholder="请输入问诊人体模型" />
        </el-form-item> -->

        <el-form-item label="图片路径" prop="bannerImage">
          <div class="container-wrapper">


            <div class="image-container">
              <!-- 大图 bggirl.png 永久显示 -->
        
              <img v-if="form.caseUsex === 1" src="/prod-api/profile/upload/moxing/bgboy.png" alt="Background Image"
                class="background-image" />

              <img v-else src="/prod-api/profile/upload/moxing/bggirl.png" alt="Background Image"
                class="background-image" />
              <!-- 小图 s.png 根据 indexg 动态显示 -->
              <img v-if="overlayIndex" src="/prod-api/profile/upload/moxing/s.png" :style="overlayImageStyle"
                alt="Overlay Image" class="overlay-image" />
            </div>

            
            <div class="image-container2">
          

              <img v-if="form.caseUsex === 1" src="/prod-api/profile/upload/moxing/bgboyback.png" alt="Background Image"
                class="background-image" />

              <img v-else src="/prod-api/profile/upload/moxing/bggirlback.png" alt="Background Image"
                class="background-image" />
           
              <img v-if="overlayIndex2" src="/prod-api/profile/upload/moxing/s.png" :style="overlayImageStyle2"
                alt="Overlay Image" class="overlay-image" />
            </div>
          </div>

        </el-form-item>




        <el-form-item label="治理前照片" prop="caseImagebefore">
          <image-upload :limit="1" v-model="form.caseImagebefore" />
        </el-form-item>
        <el-form-item label="治理后照片" prop="caseImageafter">
          <image-upload :limit="1" v-model="form.caseImageafter" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCase, getCase, delCase, addCase, updateCase } from "@/api/ask/case";
import { dropList } from "@/api/om/channel";

export default {
  name: "Case",
  data() {
    return {
      totalImages: 60,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 病例管理表格数据
      caseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        caseUuid: null,
        caseCode6: null,
        caseOpenid: null,
        casePhone: null,
        caseUname: null,
        caseUage: null,
        caseUsex: null,
        caseText: null,
        caseRemark: null,
        caseChannelid: null,
        caseFrontpoint: null,
        caseBackpoint: null,
        caseImagebefore: null,
        caseImageafter: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        caseCreatetime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        caseUpdatetime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      },
      channelOptions: [],
      channelParam:{}
    };
  },
  computed: {
    //
    overlayIndex: {
      get() {
        console.log("正正");
        // 如果 caseFrontpoint 是有效的数字，则返回该数字，否则返回默认值
        const parsedValue = parseInt(this.form.caseFrontpoint, 10); //十进制数系统
        return isNaN(parsedValue) ? 1 : parsedValue; // 默认值为1
      },
      set(newValue) {
        // 当 overlayIndex 被修改时更新 form.caseFrontpoint
        this.form.caseFrontpoint = newValue;
      }
    },
    overlayIndex2: {
      get() {
        console.log("背背");
        // 如果 caseFrontpoint 是有效的数字，则返回该数字，否则返回默认值
        const parsedValue = parseInt(this.form.caseBackpoint, 10);
        return isNaN(parsedValue) ? 1 : parsedValue; // 默认值为1
      },
      set(newValue) {
        // 当 overlayIndex 被修改时更新 form.caseFrontpoint
        this.form.caseBackpoint = newValue;
      }
    },

    // 将图片数组分组成每组5个元素的二维数组
    // 计算每个格子的宽度和高度
    cellWidth() {
      return 266 / 5; // 宽度为266px，分成5份
    },
    cellHeight() {
      return 525 / 12; // 高度为525px，分成12份
    },
    // 根据 overlayIndex 计算小图的位置
    overlayImageStyle() {
      const position = this.calculatePosition(this.overlayIndex);
      return {
        position: 'absolute',
        left: `${position.x}px`,
        top: `${position.y}px`,
        width: `${this.cellWidth}px`,
        height: `${this.cellHeight}px`,
        pointerEvents: 'none', // 确保叠加图片不会影响点击事件
      };
    },
    overlayImageStyle2() {
      const position = this.calculatePosition(this.overlayIndex2);
      return {
        position: 'absolute',
        left: `${position.x}px`,
        top: `${position.y}px`,
        width: `${this.cellWidth}px`,
        height: `${this.cellHeight}px`,
        pointerEvents: 'none', // 确保叠加图片不会影响点击事件
      };
    }


  },
  created() {
    this.getList();
  },
  methods: {
    // 跳转治疗页面
    goTreat(row) {
      this.$router.push({ path: '/askmm/treat', query: row })
      // localStorage.setItem('modalShown', 'true');
    },
    ///
    calculatePosition(indexg) {
      const adjustedIndex = indexg - 1;
      const column = adjustedIndex % 5; // 列数
      const row = Math.floor(adjustedIndex / 5); // 行数
      const x = column * this.cellWidth;
      const y = row * this.cellHeight;
      return { x, y };
    },

    /** 查询病例管理列表 */
    getList() {
      this.loading = true;
      listCase(this.queryParams).then(response => {
        //
        this.mpuserList = response.rows.map(item => {
                // 基于mpuserAge值来判断性别并添加mpuserSex_Text属性
                // item.mpuserSex_Text = item.mpuserAge === 1 ? '男' : ['女', '女'][item.mpuserAge - 2];
                // return item;
                item.caseUsex_Text = item.caseUsex === 1 ? '男' : ['女', '女'][item.caseUsex - 2];
                return item;
            });
        this.caseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        caseId: null,
        caseUuid: null,
        caseCode6: null,
        caseOpenid: null,
        casePhone: null,
        caseUname: null,
        caseUage: null,
        caseUsex: null,
        caseText: null,
        caseRemark: null,
        caseChannelid: null,
        caseFrontpoint: null,
        caseBackpoint: null,
        caseImagebefore: null,
        caseImageafter: null,
        caseCreatetime: null,
        caseUpdatetime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.caseId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.queryDrop(); // 获取渠道下拉列表
      this.open = true;
      this.title = "添加病例管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const caseId = row.caseId || this.ids
      getCase(caseId).then(response => {
        this.form = response.data;
        this.queryDrop(); // 获取渠道下拉列表
        this.open = true;
        this.title = "修改病例管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        //console.log(valid)  校验所有项
        if (valid) {
          if (this.form.caseId != null) {
            updateCase(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCase(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const caseIds = row.caseId || this.ids;
      this.$modal.confirm('是否确认删除病例管理编号为"' + caseIds + '"的数据项？').then(function () {
        return delCase(caseIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ask/case/export', {
        ...this.queryParams
      }, `case_${new Date().getTime()}.xlsx`)
    },
    // 渠道下拉
    queryDrop() {
      dropList().then(res => {
        this.channelOptions = res.data
      })
    }
  }
};
</script>

<style scoped>
.image-container {
  position: relative;
  width: 266px;
  /* 大图的实际宽度 */
  height: 525px;
  /* 大图的实际高度 */
}

.image-container2 {
  position: relative;
  width: 266px;
  /* 大图的实际宽度 */
  height: 525px;
  /* 大图的实际高度 */
}

.background-image {
  width: 100%;
  height: 100%;
  display: block;
}

.overlay-image {
  position: absolute;
}


.container-wrapper {
  display: flex;
  justify-content: space-between;
  /* 或者使用space-around, center等根据需要 */
}

.image-container,
.image-container2 {
  margin: 10px;
  /* 根据需要调整 */
}
</style>
